package com.zbkj.service.service.impl.splittingAccount;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.common.model.shop.OrderAdvertising;
import com.zbkj.service.dao.goods.AdOrderAdvertisingDao;
import com.zbkj.service.service.AdOrderAdvertisingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor
public class AdOrderAdvertisingServiceImpl extends ServiceImpl<AdOrderAdvertisingDao, OrderAdvertising>
        implements AdOrderAdvertisingService {

    @Override
    @DS("pointarchive")
    public int selectProductCount(Integer productId, Integer merType) {
        int sale = this.baseMapper.selectCount(new QueryWrapper<OrderAdvertising>()
                .lambda()
                .eq(OrderAdvertising::getProductId, productId)
                .eq(OrderAdvertising::getMerType, merType)
                .eq(OrderAdvertising::getIsSale, true)
        );
        return sale;
    }

    /**
     * 查询 广告投放信息
     *
     * @param productId
     * @param merType
     * @return
     */
    @Override
    @DS("pointarchive")
    public OrderAdvertising get(Integer productId, Integer merType) {
        //查询可开卖的投放广告信息
        OrderAdvertising orderAdvertising = this.baseMapper.selectOne(new QueryWrapper<OrderAdvertising>()
                .lambda()
                .eq(OrderAdvertising::getProductId, productId)
                .eq(OrderAdvertising::getMerType, merType)
                .eq(OrderAdvertising::getIsSale, true)
                .last("limit 1")
        );
        return orderAdvertising;
    }

    /**
     * 查询对应商品是否有广告
     *
     * @param productId    产品id
     * @param day          多少天前的订单
     * @param merTypeArray 类型
     * @return
     */
    @Override
    @DS("pointarchive")
    public OrderAdvertising getSplitOrderAdvertising(Integer productId, Integer day, Integer... merTypeArray) {
        //计算30天前的时间
        LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(day);

        //查询可开卖的投放广告信息
        OrderAdvertising orderAdvertising = this.baseMapper.selectOne(new QueryWrapper<OrderAdvertising>()
                .lambda()
                .eq(OrderAdvertising::getProductId, productId)
                .in(OrderAdvertising::getMerType, merTypeArray)
                .eq(OrderAdvertising::getIsSale, true)
                .between(OrderAdvertising::getCreateTime, thirtyDaysAgo, new Date())
                .last("limit 1")
        );
        return orderAdvertising;
    }
}